package pri.lt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import pri.lt.bean.Label;
import pri.lt.client.LabelClient;
import pri.lt.client.UserClient;

import java.io.*;
import java.util.List;
import java.util.Properties;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class DataInitApp
{



    public static void main( String[] args ) throws Exception {
        System.out.println( "Hello World!" );




        UserClient.login();

        //initLabels();

        updateHotContents();
    }

    private static void initLabels() throws Exception {
        InputStream in = DataInitApp.class.getClassLoader().getResourceAsStream("all_cat");
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = bufferedReader.readLine();
        while (line != null) {
            line = line.trim();
            System.out.println(line);
            addLabel(line);
            line = bufferedReader.readLine();
        }
    }

    private static String getParentLabelName(String labelName) {
        if (labelName != null && labelName.lastIndexOf("/") > 0) {
            return labelName.substring(0, labelName.lastIndexOf("/"));
        }

        return null;
    }

    private static void addLabel(String labelName) {
        String parentName = getParentLabelName(labelName);
        if (parentName != null) {
            parentName = parentName.trim();
        }
        String labelId = LabelClient.getLabelIdByName(labelName);
        if (labelId == null || labelId.trim().length() > 0) {
            String parentLabelId = null;
            if (parentName != null) {
                parentLabelId = LabelClient.getLabelIdByName(parentName);
                if(parentLabelId == null || parentLabelId.trim().length() == 0) {
                    addLabel(parentName);
                }
                parentLabelId = LabelClient.getLabelIdByName(parentName);
            }
            LabelClient.addLabel(labelName, parentLabelId);
        }

    }

    private static void updateHotContents() {
        List<Label> labelList = LabelClient.getAllLabelIds();
        if (labelList != null) {
            for (Label label : labelList) {
                LabelClient.updateHotContent("" + label.getId());
            }
        }
    }

}
